package com.xddcodec.fs.file.controller;

import com.xddcodec.fs.file.domain.dto.CreateShareCmd;
import com.xddcodec.fs.file.domain.dto.VerifyShareCodeCmd;
import com.xddcodec.fs.file.domain.qry.FileShareQry;
import com.xddcodec.fs.file.domain.vo.FileShareAccessRecordVO;
import com.xddcodec.fs.file.domain.vo.FileShareThinVO;
import com.xddcodec.fs.file.domain.vo.FileShareVO;
import com.xddcodec.fs.file.domain.vo.FileVO;
import com.xddcodec.fs.file.service.FileShareAccessRecordService;
import com.xddcodec.fs.file.service.FileShareService;
import com.xddcodec.fs.framework.common.domain.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Validated
@Slf4j
@RestController
@RequestMapping("/apis/share")
@Tag(name = "文件分享", description = "文件分享")
public class FileShareController {

    @Autowired
    private FileShareService fileShareService;

    @Autowired
    private FileShareAccessRecordService fileShareAccessRecordService;

    @GetMapping("/list")
    @Operation(summary = "获取我的分享", description = "获取我的分享列表")
    public Result<List<FileShareVO>> getList(FileShareQry qry) {
        List<FileShareVO> result = fileShareService.getList(qry);
        return Result.ok(result);
    }

    @GetMapping("/{shareId}")
    @Operation(summary = "获取分享详细信息", description = "获取分享详细信息")
    public Result<FileShareVO> getDetail(@PathVariable String shareId) {
        FileShareVO result = fileShareService.getDetail(shareId);
        return Result.ok(result);
    }

    @GetMapping("/{shareId}/access/records")
    @Operation(summary = "获取分享访问记录列表", description = "获取分享访问记录列表")
    public Result<List<FileShareAccessRecordVO>> getListByShareId(@PathVariable String shareId) {
        List<FileShareAccessRecordVO> result = fileShareAccessRecordService.getListByShareId(shareId);
        return Result.ok(result);
    }

    @PostMapping("/create")
    @Operation(summary = "创建分享", description = "创建分享")
    public Result<FileShareVO> createDirectory(@RequestBody @Validated CreateShareCmd cmd) {
        FileShareVO fileShareVO = fileShareService.createShare(cmd);
        return Result.ok(fileShareVO);
    }

    @DeleteMapping("/cancels")
    @Operation(summary = "取消分享", description = "取消分享")
    public Result<FileShareVO> cancelShares(@RequestBody List<String> ids) {
        fileShareService.cancelShares(ids);
        return Result.ok();
    }

    @Operation(summary = "验证提取码", description = "验证提取码")
    @PostMapping("/verify/code")
    public Result<Boolean> verifyShareCode(@RequestBody @Validated VerifyShareCodeCmd cmd) {
        boolean result = fileShareService.verifyShareCode(cmd);
        return Result.ok(result);
    }

    @Operation(summary = "获取分享页数据", description = "获取分享页数据")
    @GetMapping("/{shareId}/info")
    public Result<FileShareThinVO> getFileShareThin(@PathVariable String shareId) {
        return Result.ok(fileShareService.getFileShareThinVO(shareId));
    }

    @Operation(summary = "获取分享页文件列表数据", description = "获取分享页文件列表数据")
    @GetMapping("/{shareId}/items")
    public Result<List<FileVO>> getShareFileItems(@PathVariable String shareId, @RequestParam(required = false) String parentId) {
        return Result.ok(fileShareService.getShareFileItems(shareId, parentId));
    }
}
